package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConst;
import com.itheima.constant.RedisConst;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import com.itheima.utils.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Reference
    OrderService orderService;

    @Autowired
    JedisPool jedisPool;

    /***
     * 步骤：
     *    1. 校验验证码，如果验证码不正确，返回错误结果
     *    2. 设置预约类型
     *    3. 添加预约信息到数据
     *    4. 发送通知短信给用户
     *
     * @param map
     * @return
     */
    @RequestMapping("/submit")
    public Result submit(@RequestBody Map<String, String> map){
        // 1. 校验验证码，如果验证码不正确，返回错误结果
        //获取手机号码
        String telephone = map.get("telephone");
        //获取redis中的验证码
        String validateCodeInRedis = jedisPool.getResource().get(RedisConst.SENDTYPE_ORDER + "-" + telephone);
        //获取参数中的验证码
        String validateCodeInParam = map.get("validateCode");
        //如果校验失败
        if(!(validateCodeInRedis != null && validateCodeInRedis.equals(validateCodeInParam))){
            return new Result(false, MessageConst.VALIDATECODE_ERROR);
        }
        //2. 设置预约类型
        map.put("orderType", Order.ORDERTYPE_WEIXIN);
        //3. 添加预约信息到数据
        Result result = null;
        try {
            result = orderService.addOrder(map);
        } catch (ParseException e) {
            e.printStackTrace();
            System.out.println("日期处理失败");
        }
        if(result.isFlag()){
            //4. 发送通知短信给用户(省略了)
//            System.out.println("发送通知短信给客户");
            try {
                SMSUtils.sendShortMessage(telephone, "123456");
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }
        return  result;

    }


    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Map<String,Object> resultMap =  orderService.findById(id);
            Date orderDate = (Date) resultMap.get("orderDate");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String orderDateStr = sdf.format(orderDate);
            resultMap.put("orderDate", orderDateStr);
            return new Result(true,MessageConst.QUERY_ORDER_SUCCESS, resultMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.QUERY_ORDER_FAIL);
        }
    }
}
